export function timeFix () {
  const time = new Date()
  const hour = time.getHours()
  return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好'
}

export function welcome () {
  const arr = ['休息一会儿吧', '准备吃什么呢?', '要不要打一把 DOTA', '我猜你可能累了']
  const index = Math.floor(Math.random() * arr.length)
  return arr[index]
}

/**
 * 触发 window.resize
 */
export function triggerWindowResizeEvent () {
  const event = document.createEvent('HTMLEvents')
  event.initEvent('resize', true, true)
  event.eventType = 'message'
  window.dispatchEvent(event)
}

export function handleScrollHeader (callback) {
  let timer = 0

  let beforeScrollTop = window.pageYOffset
  callback = callback || function () {}
  window.addEventListener(
    'scroll',
    event => {
      clearTimeout(timer)
      timer = setTimeout(() => {
        let direction = 'up'
        const afterScrollTop = window.pageYOffset
        const delta = afterScrollTop - beforeScrollTop
        if (delta === 0) {
          return false
        }
        direction = delta > 0 ? 'down' : 'up'
        callback(direction)
        beforeScrollTop = afterScrollTop
      }, 50)
    },
    false
  )
}

export function isIE () {
  const bw = window.navigator.userAgent
  const compare = (s) => bw.indexOf(s) >= 0
  const ie11 = (() => 'ActiveXObject' in window)()
  return compare('MSIE') || ie11
}

/**
 * Remove loading animate
 * @param id parent element id or class
 * @param timeout
 */
export function removeLoadingAnimate (id = '', timeout = 1500) {
  if (id === '') {
    return
  }
  setTimeout(() => {
    document.body.removeChild(document.getElementById(id))
  }, timeout)
}
export function scorePassword (pass) {
  let score = 0
  if (!pass) {
    return score
  }
  // award every unique letter until 5 repetitions
  const letters = {}
  for (let i = 0; i < pass.length; i++) {
      letters[pass[i]] = (letters[pass[i]] || 0) + 1
      score += 5.0 / letters[pass[i]]
  }

  // bonus points for mixing it up
  const variations = {
      digits: /\d/.test(pass),
      lower: /[a-z]/.test(pass),
      upper: /[A-Z]/.test(pass),
      nonWords: /\W/.test(pass)
  }

  let variationCount = 0
  for (var check in variations) {
      variationCount += (variations[check] === true) ? 1 : 0
  }
  score += (variationCount - 1) * 10

  return parseInt(score)
}

/**
 * 简化 input decorator
 * @param id
 * @param option | message
 * @returns {(*|{rules: [{message: string, required: boolean}]})[]}
 */
export function decoratorRequired(id, option) {
  let optionNext = {
    rules: [
      {required: true, message: '必填项'},
      // { validator: handleUsernameOrEmail },
    ],
    validateTrigger: 'change'
  };

  if (_.isString(option)) {
    optionNext.rules[0].message = option;
  } else {
    _.each(option, (value, key) => {
      if (key === 'rules') {
        optionNext.rules.push(value);
      } else {
        optionNext.key = value;
      }
    });
  }

  return [id, optionNext];
}

/**
 * @description: 获取日月时分
 * @param { Date } time 时间戳
 * @return {*}
 */
export function timeFormat (time = 0) {
  if (time === '' || time === null) {
    return '--'
  } else {
    let date = (typeof time === 'string') ? (new Date(time)) : (new Date(+time))
    return `${pad0(date.getMonth() + 1)}-${pad0(date.getDate())} ${pad0(date.getHours())}:${pad0(date.getMinutes())}`
  }
}
/**
 * @description: 获取日月时分秒
 * @param { Date } time 时间戳
 * @return {*}
 */
export function timeFormat2 (time = 0) {
  if (time === '' || time === null) {
    return '--'
  } else {
    let date = (typeof time === 'string') ? (new Date(time)) : (new Date(+time))
    return `${pad0(date.getMonth() + 1)}-${pad0(date.getDate())} ${pad0(date.getHours())}:${pad0(date.getMinutes())}:${pad0(date.getSeconds())}`
  }
}

/**
 * @description: 获取年月日
 * @param {*} time
 * @return {*}
 */
export function timeFormat3 (time = 0) {
  if (time === '' || time === null) {
    return '--'
  } else {
    let date = (typeof time === 'string') ? (new Date(time)) : (new Date(+time))
    return `${pad0(date.getFullYear())}-${pad0(date.getMonth() + 1)}-${pad0(date.getDate())}`
  }
}

/**
 * @description: 获取年月日前一天
 * @param {*} timeB
 * @return {*}
 */
 export function timeFormat3B (time = 0) {
  if (time === '' || time === null) {
    return '--'
  } else {
    let time2 = new Date(time).getTime() - 3600 * 24 * 1000
    let date = (typeof time2 === 'string') ? (new Date(time2)) : (new Date(+time2))
    return `${pad0(date.getFullYear())}-${pad0(date.getMonth() + 1)}-${pad0(date.getDate())}`
  }
}


/**
 * @description: 获取时分秒
 * @param {*} time
 * @return {*}
 */
export function timeFormat4 (time = 0) {
  if (time === '' || time === null) {
    return '--'
  } else {
    let date = (typeof time === 'string') ? (new Date(time)) : (new Date(+time))
    return `${pad0(date.getHours())}:${pad0(date.getMinutes())}:${pad0(date.getSeconds())}`
  }
}

/**
 * @description: 获取年月
 * @param {*} time
 * @return {*}
 */
 export function timeFormat5 (time = 0) {
  if (time === '' || time === null) {
    return '--'
  } else {
    let date = (typeof time === 'string') ? (new Date(time)) : (new Date(+time))
    return `${pad0(date.getFullYear())}-${pad0(date.getMonth() + 1)}`
  }
}

/**
 * 在字符串前补零
 * @param {String} str 原字符串
 * @param {Number} count 补零个数
 */
function pad0 (str = '', count = 2) {
  let padStr = ''
  str = str + ''
  let add = count - str.length
  if (add > 0) {
    for (let i = 0; i < add; ++i) {
      padStr += '0'
    }
  }
  return padStr + str
}
/**
 * @description:  全屏dom
 * @param { DOM } docElm
 * @return {*}
 */
export function fullScreen (docElm) {
  //W3C
  if(docElm.requestFullscreen) {
    docElm.requestFullscreen()
  }
  //FireFox
  else if(docElm.mozRequestFullScreen) {
    docElm.mozRequestFullScreen()
  }
  //Chrome等
  else if(docElm.webkitRequestFullScreen) {
    docElm.webkitRequestFullScreen()
  }
  //IE11
  else if(docElm.msRequestFullscreen) {
    docElm.msRequestFullscreen()
  }
}

export function swap(arr, i, j) {
  const temp = arr[i]
  arr[i] = arr[j]
  arr[j] = temp
}

/* 四舍五入保持两位小数 */
export function twoFixed (data) {
  return (Math.round(data * 100000000) / 100000000)
}

/* 四舍五入保持两位小数，单位万元 */
export function twoFixedW (data) {
  return (Math.round(data * 100000000) / 100000000) * 100 / 1000000
}